﻿#include "WitchDoctor.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    WitchDoctor::WitchDoctor(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool WitchDoctor::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    void WitchDoctor::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        if (getEnvir()->Random->Next(5) == 0)
        {
            TeleportRandom(10, AttackRange);
        }
        else
        {
            auto hpPercent = (HP * 100) / Stats[Stat::HP];

            if (getEnvir()->Random->Next(3) == 0 && hpPercent < 50)
            {
                S::ObjectAttack *tempVar = new S::ObjectAttack();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                tempVar->Type = 0;
                Broadcast(tempVar);

                ActionTime = getEnvir()->getTime() + 300;
                AttackTime = getEnvir()->getTime() + AttackSpeed;

                ChangeHP(Stats[Stat::HP] / 4);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
                S::ObjectRangeAttack *tempVar2 = new S::ObjectRangeAttack();
                tempVar2->ObjectID = ObjectID;
                tempVar2->Direction = getDirection();
                tempVar2->Location = getCurrentLocation();
                tempVar2->TargetID = getTarget()->ObjectID;
                Broadcast(tempVar2);

                ActionTime = getEnvir()->getTime() + 300;
                AttackTime = getEnvir()->getTime() + AttackSpeed;

                int damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC]);
                if (damage == 0)
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                int delay = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation()) * 50 + 500; //50 MS per Step

                DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + delay, {getTarget(), damage, DefenceType::MACAgility});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
        }
    }

    void WitchDoctor::ProcessTarget()
    {
        if (getTarget() == nullptr || !getCanAttack())
        {
            return;
        }

        if (InAttackRange() && getEnvir()->getTime() < FearTime)
        {
            Attack();
            return;
        }

        FearTime = getEnvir()->getTime() + 5000;

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        int dist = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation());

        if (dist >= AttackRange)
        {
            MoveTo(getTarget()->getCurrentLocation());
        }
        else
        {
            MirDirection dir = Functions::DirectionFromPoint(getTarget()->getCurrentLocation(), getCurrentLocation());

            if (Walk(dir))
            {
                return;
            }

            switch (getEnvir()->Random->Next(2)) //No favour
            {
                case 0:
                    for (int i = 0; i < 7; i++)
                    {
                        dir = Functions::NextDir(dir);

                        if (Walk(dir))
                        {
                            return;
                        }
                    }
                    break;
                default:
                    for (int i = 0; i < 7; i++)
                    {
                        dir = Functions::PreviousDir(dir);

                        if (Walk(dir))
                        {
                            return;
                        }
                    }
                    break;
            }

        }
    }

    bool WitchDoctor::TeleportRandom(int attempts, int distance, Map *temp)
    {
        if (getTarget() == nullptr)
        {
            return false;
        }

        for (int i = 0; i < attempts; i++)
        {
            Point *location;

            location = new Point(getTarget()->getCurrentLocation()->X + getEnvir()->Random->Next(-distance, distance + 1), getTarget()->getCurrentLocation()->Y + getEnvir()->Random->Next(-distance, distance + 1));

            if (Teleport(getCurrentMap(), location, true, 5))
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete location' statement was not added since location was passed to a method or constructor. Handle memory management manually.
                return true;
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete location' statement was not added since location was passed to a method or constructor. Handle memory management manually.
        }

        return false;
    }
}
